<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>templateExists()</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="api.functions.html" title="Chapter 14. Smarty Class Methods">
<link rel="prev" href="api.set.template.dir.html" title="setTemplateDir()">
<link rel="next" href="api.unregister.cacheresource.html" title="unregisterCacheResource()">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">templateExists()</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="api.set.template.dir.html">Prev</a> </td>
<th width="60%" align="center">Chapter 14. Smarty Class Methods</th>
<td width="20%" align="right"> <a accesskey="n" href="api.unregister.cacheresource.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="refentry" title="templateExists()">
<a name="api.template.exists"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>templateExists() — checks whether the specified template exists</p>
</div>
<div class="refsect1" title="Description">
<a name="id440301"></a><h2>Description</h2>
<code class="methodsynopsis"><span class="type">bool </span><span class="methodname">templateExists</span>(<span class="methodparam"><span class="type">string </span><span class="parameter">template</span></span>);</code><p>
   It can  accept either a path to the template on the filesystem or a
   resource string specifying the template.
  </p>
<div class="example">
<a name="id440790"></a><p class="title"><b>Example 14.48. templateExists()</b></p>
<div class="example-contents">
<p>
    This example uses <code class="literal">$_GET['page']</code> to
    <a class="link" href="language.function.include.html" title="{include}"><code class="varname">{include}</code></a>
    a content template. If the template does not exist then an error page
    is displayed instead. First the <code class="filename">page_container.tpl</code>
    </p>
<pre class="programlisting">

&lt;html&gt;
&lt;head&gt;&lt;title&gt;{$title}&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
{include file='page_top.tpl'}

{* include middle content page *}
{include file=$content_template}

{include file='page_footer.tpl'}
&lt;/body&gt;

  </pre>
<p>
  And the php script
  </p>
<pre class="programlisting">

&lt;?php

// set the filename eg index.inc.tpl
$mid_template = $_GET['page'].'.inc.tpl';

if( !$smarty-&gt;templateExists($mid_template) ){
    $mid_template = 'page_not_found.tpl';
}
$smarty-&gt;assign('content_template', $mid_template);

$smarty-&gt;display('page_container.tpl');

?&gt;

  </pre>
</div>
</div>
<br class="example-break"><p>
  See also
  <a class="link" href="api.display.html" title="display()"><code class="varname">display()</code></a>,
  <a class="link" href="api.fetch.html" title="fetch()"><code class="varname">fetch()</code></a>,
  <a class="link" href="language.function.include.html" title="{include}"><code class="varname">{include}</code></a>
  and
  <a class="link" href="language.function.insert.html" title="{insert}"><code class="varname">{insert}</code></a>
  </p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="api.set.template.dir.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="api.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="api.unregister.cacheresource.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">setTemplateDir() </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> unregisterCacheResource()</td>
</tr>
</table>
</div>
</body>
</html>
